<?php
namespace Agent\Controller;
use Think\Controller;
use Think\Image;
class AuditController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//商务列表
    public function index(){
        $admin_id = $_SESSION["agentInfo"]["admin_id"];
        if(empty($admin_id)){
            $this->error('信息错误');exit;
        }
        $where['w.admin_id'] = $admin_id;
        $where['w.status'] = 1;
        $search = I('search');
        if($search){
            $where['u.user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $header = C('header')['group'];

        $counts = M('worlddetails')
                ->alias("w")
                ->join("left join pt_user as u on w.user_id=u.user_id")
                ->field('w.id,u.user_name,w.seo,count(w.keyword),w.logtime,w.status')
                ->where($where)
                ->group('u.user_name,w.seo')
                ->order('w.id desc')
                ->select();
        $count = count($counts);

        // $count = M('worlddetails')
        //             ->alias("w")
        //             ->join("left join pt_user as u on w.user_id=u.user_id")
        //             ->field('w.id,u.user_name,w.site,w.keyword,w.seo,w.logtime,w.amount,w.price,w.standard,w.standard*w.price,u.status')
        //             ->where($where)
        //             ->count();


        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出


        $list = M('worlddetails')
                    ->alias("w")
                    ->join("left join pt_user as u on w.user_id=u.user_id")
                    ->field('w.id,u.user_name,w.seo,count(w.keyword),w.logtime,w.status,w.user_id')
                    ->where($where)
                    ->group('u.user_id,w.seo')
                    ->order('w.id desc')
                    ->limit($Page->firstRow . ',' . $Page->listRows)
                    ->select();
        $seo = [1=>'百度PC','百度移动','360PC','360移动','搜狗PC','搜狗移动'];              
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
            $list[$k]['seo'] = $seo[$v['seo']];
            $list[$k]['status'] = '进行中';
            $list[$k]['user_id'] = "<a href='#' class='details' user_id='{$v['user_id']}' seo='{$v['seo']}'>查看详情</a>";;
        }

        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Audit');
        layout('Layout/twolayout');
        $this->display();
    }


    public function details(){
        $user_id = I('user_id');
        $seo = I('seo');
        if(!$user_id || !$seo){
            $this->ajaxreturn(array('state' => 2, 'data' => '信息错误1'), 'json');
        }

        $where['c.uid'] = $user_id;
        $where['c.seo'] = $seo;
        $where['c.type'] = 2;

        $logtime = strtotime(date('Y-m-d',time()));
        $where['c.logtime'] = array('gt',$logtime);
        $list = M('ucharge')
                ->alias("c")
                ->join("left join pt_user as u on u.user_id=c.uid")
                ->join("left join pt_keyword as k on k.id=c.keyword_id")
                ->field('c.charge_id,u.user_name,k.Keyword,c.logtime,c.desc,c.amount,c.desc as note')
                ->order('c.charge_id desc')
                ->where($where)
                ->select();
        if($list){
            $n = 1;
            foreach ($list as $k => $v) {
                $list[$k]['charge_id'] = $n;
                $list[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
                $list[$k]['desc'] = '排名消费';
                $list[$k]['note'] = $v['log_date'].'任务达标扣费';
                $n++;
            }
            $this->ajaxreturn(array('state' => 1, 'data' => $list), 'json');exit;
        }else{
            $this->ajaxreturn(array('state' => 2, 'data' => '数据为空'), 'json');exit;
        }
    }
}